package com.zhuqieli.database_learn.controller;

import com.zhuqieli.database_learn.common.Result;
import com.zhuqieli.database_learn.pojo.FileTable;
import com.zhuqieli.database_learn.pojo.UserTable;
import com.zhuqieli.database_learn.service.FileTableService;
import com.zhuqieli.database_learn.service.UserTableService;
import com.zhuqieli.database_learn.utils.BcryptUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.context.Context;

import java.util.List;

/**
 * @author dupr
 * @description 用户服务接口
 * @date 2023/8/30 19:24:45
 */

@Slf4j
@Controller
@RequestMapping("/user")
public class UserTableController {
    @Autowired
    private UserTableService userTableService;

    @Autowired
    private FileTableService fileTableService;

    //拦截器测试用接口，需将@Controller改为@RestController
    @GetMapping("/gate/sun")
    public void gate() {
        System.out.println("今日很好阳光");
    }

    @PostMapping("/register")
    public String register(@RequestParam(value = "phoneNumber") String phoneNumber,
                                      @RequestParam(value = "password") String password,
                                      @RequestParam(value = "username") String username) {
        UserTable userTable = UserTable.builder().phoneNumber(phoneNumber)
                                       .password(password)
                                       .name(username).build();
        userTable.setPassword(BcryptUtil.encode(userTable.getPassword()));
        //todo 校验
        userTableService.save(userTable);
        return "login";
    }

    @PostMapping("/login")
    public String login(@RequestParam String phoneNumber, @RequestParam String password,
                        Model model) {
        // 从数据库中查找用户的信息，信息正确生成token
        UserTable user = userTableService.login(phoneNumber, password);

        //用户不存在
        if("99999999999".equals(user.getPhoneNumber())){
            return "404";
        }

        //密码错误
        if("88888888888".equals(user.getPhoneNumber())){
            return "403";
        }

        Context context = new Context();
        context.setVariable("userId", user.getId());
        context.setVariable("phoneNumber", user.getPhoneNumber());
        context.setVariable("name", user.getName());
        //实际为jwtToken
        context.setVariable("Authorization", user.getPassword());

        model.addAttribute("happy", context);

        List<FileTable> files = fileTableService.selectMyfiles(user.getId());

        model.addAttribute("files", files);

        return "users";

    }

    @GetMapping("/detail")
    public Result detail(@RequestParam String id) {
        try {
            return Result.success(userTableService.getById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail(e.getMessage());
        }
    }

    @GetMapping("/auth")
    public String auth() {
        return "已成功登录";
    }

    @GetMapping("/role")
    @RequiresRoles("vip")
    public String role() {
        return "测试Vip角色";
    }

    @GetMapping("/permission")
    @RequiresPermissions(value = {"add", "update"}, logical = Logical.AND)
    public String permission() {
        return "测试Add和Update权限";
    }
}
